package com.example.ck.controller;

import com.example.ck.dto.UserDto;
import com.example.ck.entity.User;
import com.example.ck.service.UserService;
import com.example.ck.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 注册用户
     *
     * @param user
     * @return
     */
    @PostMapping("/register")
    public ResultVo RegisterUser(@RequestBody User user) {
        boolean flag = userService.checkName(user.getUsername());
        ResultVo vo = new ResultVo<>();
        if (!flag) {
            boolean flag2 = userService.registerUser(user);
            if (flag2) {
                vo.setMessage("注册成功");
                vo.setSuccess(true);
                vo.setCode(200);
                vo.setData(user);
            } else {
                vo.setMessage("注册失败");
                vo.setSuccess(false);
                vo.setCode(201);
            }
        } else {
            vo.setMessage("当前用户名已被占用");
            vo.setSuccess(false);
            vo.setCode(201);
        }
        return vo;
    }

    /**
     * 登录用户
     */
    @PostMapping("/login")
    public ResultVo userLogin(@RequestBody Map<String, String> map) {
        ResultVo vo = new ResultVo<>();
        String username = map.get("username");
        String password = map.get("password");
        String type = map.get("type");
        boolean flag = userService.checkName(username);
        if (flag) {
            // 查询到当前账户名
            UserDto userDto = userService.queryUser(username, password, type);
            if (userDto != null) {
                vo.setMessage("登录成功");
                vo.setSuccess(true);
                vo.setData(userDto);
                vo.setCode(200);
            } else {
                vo.setMessage("账户密码或用户类型错误");
                vo.setSuccess(true);
                vo.setCode(201);
            }
        } else {
            vo.setMessage("当前账户还没有注册");
            vo.setSuccess(false);
            vo.setCode(201);
        }
        return vo;
    }

    /**
     * 根据用户名获取用户所有信息
     */
    @GetMapping("/find")
    public ResultVo findUserByName(@RequestParam String username) {
        User user = userService.findUser(username);
        return new ResultVo(user);
    }

    /**
     * 用户根据id修改信息
     */
    @PostMapping("/update")
    public ResultVo updateUserInfo(@RequestParam Integer id, @RequestBody Map<String, String> map) {
        String header = map.get("header");
        String username = map.get("username");
        String professional = map.get("professional");
        String password = map.get("password");
        String email = map.get("email");
        String phone = map.get("phone");

        ResultVo vo = new ResultVo<>();
        boolean b = userService.updateUserInfo(id, header, username, professional, password, email, phone);
        if (b) {
            vo.setMessage("用户信息修改成功");
            vo.setData(map);
            vo.setSuccess(true);
            vo.setCode(200);
        } else {
            vo.setMessage("用户信息修改失败");
            vo.setSuccess(false);
            vo.setCode(201);
        }
        return vo;
    }

    /**
     * 管理员查询所有用户
     */
    @GetMapping("/admin/queryAll")
    public ResultVo queryAllUser() {
        List<User> users = userService.queryAllUser();
        ResultVo vo = new ResultVo<>();
        if (users != null) {
            vo.setMessage("查询所有用户成功");
            vo.setSuccess(true);
            vo.setCode(200);
            vo.setData(users);
        } else {
            vo.setMessage("查询所有用户失败");
            vo.setSuccess(false);
            vo.setCode(201);
        }
        return vo;
    }

    /**
     * 管理员根据id移除用户
     */
    @DeleteMapping("/admin/remove")
    public ResultVo removeUserById(Integer id) {
        boolean b = userService.removeUserById(id);
        ResultVo vo = new ResultVo<>();
        if (b) {
            vo.setMessage("用户移除成功");
            vo.setSuccess(true);
            vo.setCode(200);
        } else {
            vo.setMessage("用户移除失败");
            vo.setSuccess(false);
            vo.setCode(201);
        }
        return vo;
    }

    /**
     * 管理员根据用户名修改用户信息
     */
    @PutMapping("/admin/update")
    public ResultVo updateUserById(@RequestParam Integer id, @RequestBody Map<String, String> map) {
        String email = map.get("email");
        String phone = map.get("phone");
        String professional = map.get("professional");
        String type = map.get("type");

        boolean b = userService.updateUserbyId(id, email, phone, professional, type);
        ResultVo vo = new ResultVo<>();
        if (b) {
            vo.setMessage("用户数据修改成功");
            vo.setSuccess(true);
            vo.setCode(200);
        } else {
            vo.setMessage("用户数据修改失败");
            vo.setSuccess(false);
            vo.setCode(201);
        }
        return vo;
    }

    /**
     * 获取所有专家用户信息
     */
    @GetMapping("/admin/expert")
    public ResultVo getExpertUserList() {
        List<User> users = userService.queryAllExpert();
       return new ResultVo<>(users);
    }
}
